<?php
class ModelStocksSector extends Model {
	public function addSector($data) {
      	$this->db->query("INSERT INTO `" . DB_PREFIX . "sectors` SET " .
      							" name = '" . $this->db->escape($data['name']) .				"', code = '" . $this->db->escape($data['code']) .      			 
				"'");

      	$sector_id = $this->db->getLastId();
      	
      	return $sector_id;
	}
	
	public function editSector($sector_id, $data) {
		$this->db->query("UPDATE `" . DB_PREFIX . "sectors` SET " .
								" name = '" . $this->db->escape($data['name']) .				"', code = '" . $this->db->escape($data['code']) .				 
				"' WHERE `sector_id` = '" . (int)$sector_id . "'");		
	}
	
	public function deleteSector($sector_id) {
      	$this->db->query("DELETE FROM `" . DB_PREFIX . "sectors` WHERE `sector_id` = '" . (int)$sector_id . "'");
	}
	
	public function getSector($sector_id) {
		
      	$query = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "sectors` WHERE `sector_id` = '" . (int)$sector_id . "'");
		
		return $query->row;
	}
	
	public function getSectors($data = array()) {
		$sql = "SELECT `sector_id`, `sector_id`,`name`,`code` FROM `" . DB_PREFIX . "sectors` WHERE 1=1 ";
		
				if (isset($data['filter_sector_id']) && !is_null($data['filter_sector_id'])) {
			$sql .= " AND sector_id LIKE '%" . $this->db->escape($data['filter_sector_id']) . "%'";
		}

		if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
			$sql .= " AND name LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
		}

		if (isset($data['filter_code']) && !is_null($data['filter_code'])) {
			$sql .= " AND code LIKE '%" . $this->db->escape($data['filter_code']) . "%'";
		}


		
		$sort_data = array(
						'sector_id',			'name',			'code',
		);	
			
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY `sector_id`";	
		}
			
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}		
		
		$query = $this->db->query($sql);
		
		return $query->rows;
	}
	
	public function getTotalSectors($data = array()) {
		$sql = "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "sectors` WHERE 1=1 ";
		
				if (isset($data['filter_sector_id']) && !is_null($data['filter_sector_id'])) {
			$sql .= " AND sector_id LIKE '%" . $this->db->escape($data['filter_sector_id']) . "%'";
		}

		if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
			$sql .= " AND name LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
		}

		if (isset($data['filter_code']) && !is_null($data['filter_code'])) {
			$sql .= " AND code LIKE '%" . $this->db->escape($data['filter_code']) . "%'";
		}


		
      	$query = $this->db->query($sql);
      	
		return $query->row['total'];
	}
	
				
}
?>